<?php
/* 
任务表格模块
审批类任务
 */
class MissionAction extends IntesysAction {
	//初始化
	public function _initialize(){
		parent::_initialize();
		C('TOKEN_ON', false);
		$_POST['site_id'] = __SITEID__;
		$this->dao = D('Mission');
	}
	public function getapply(){
		$this->pagesize = 5;//直接上级或者审批人查看
		$this->listmap = "form_id = 0 and site_id = ". __SITEID__." and member_id = ".$this->memberInfo['id'];
		$this->listorder = 'id desc';
		$this->limit=5;
		$this->initView();
		$this->dao->_limit($this->view,$this->listmap,$this->listorder,$this->limit);
		$this->display();
	}
	//显示可选择表格，用户从这里选择一个电子表格  or approver_id = '".$this -> memberInfo['id']."' or  other_id like '%".$this -> memberInfo['id']."%'
	public function index(){
		
		if($this->_param('type')&&$this->_param('type')=='0'){//list
			$this->pagesize =15;
		}else{//index
			$this->pagesize = 8;//直接上级或者审批人查看
		}
		$InteType = M('Integral_type') -> where(array('site_id' => __SITEID__)) -> select();
		$InteTypeList = array();
		foreach($InteType as $k => $v){
			$InteTypeList[$v['typename']] = $v['remark'];
		}
		
		$this->InteTypeList = $InteTypeList;
		//state >= 0 and
		$this->listmap = " site_id = ". __SITEID__." and (approver_id =".$this->memberInfo['id']." or reviewer_id=".$this->memberInfo['id']." or approver like '%,".$this->memberInfo['id'].",%')";
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			if($this->_param('mymission')){
				$this->listmap.=" and ( proposername like '%".$keywords."%' or title like '%".$keywords."%' ) ";
			}else{	
				$this->listmap.=" and ( proposername like '%".$keywords."%' or approvername like '%".$keywords."%' or title like '%{$keywords}%' ) ";
			}
			$this->keywords=$keywords;
		}
		if($this->_param('formcategory_id')){
			$id=$this->_param('formcategory_id');
			$this->listmap.=' and formcategory_id = '.$id;
		}
		if($this->_param('pagesize')){
			$this->pagesize=$this->_param('pagesize');
			$this->assign('pagesize',$this->pagesize);
		}
//		if($this->_param('id')){
//			$id=$this->_param('id');
//			$this->listmap.= ' and formcategory_id = '.$id;
//		}
        if($this->_param('status')&&$this->_param('status')!=99||$this->_param('status')=='0'){

            $this->listmap.=" and state = ".$this->_param('status');
            $this->status=$this->_param('status');
        }
        if ($this->_param('starttime') || $this->_param('endtime')) {
            $stime = strtotime($this->_param('starttime'));
            $etime = strtotime($this->_param('endtime')) + 24 * 3600;
            if ($this->_param('starttime')) {
                $this->listmap .= " and createtime >= '" . $stime . "'";
                $this->starttime=date('Y-m-d H:i:s',$stime);
            }
            if ($this->_param('endtime')) {
                $this->listmap .= " and createtime <= '" . $etime . "'";
                $this->endtime	=date('Y-m-d H:i:s',$etime);
            }
            if ($this->_param('starttime') && $this->_param('endtime')) {
                $this->listmap .= " and createtime <= '" . $etime . "'";
                $this->listmap .= " and createtime >= '" . $stime . "'";
                $this->starttime=date('Y-m-d H:i:s',$stime);
                $this->endtime	=date('Y-m-d H:i:s',$etime);
            }
        }
		
		if(isset($_GET['state']) && intval($_GET['state']) >= 0){
			if($_GET['state'] != 'all'){
				$this->listmap .= " and state = '" . intval($_GET['state']) . "'";
			}else{
				$this->listmap .= " and  state <> -1";	
			}
		}else{
			//$this->listmap .= " and state <> -1 ";
			//默认显示示审批的
			$this->listmap .= " and  ( state = 2 or state =1 ) ";
		}
		$category=D('formcategory')->select();//查找分类
		foreach($category as $key => $value){
			$map=$this->listmap;
			$map .= ' and formcategory_id = '.$value['id'];
			$category[$key]['count']=D('mission')->where($map)->count();
		}
		$this->assign('candy',$category);//分类渲染
		$this->listorder='state,id desc';

		
		if($this->_param('sorttype')){
			$sorttype=$this->_param('sorttype');
			$sorttype=explode('_',$sorttype);
			$typeid=$sorttype[0];//排序字段
			$typeval=$sorttype[1];//排序方式 1正2倒
		}
		if($typeid != ''){
			switch($typeid){
				case 4 :
					$sortby="memberstring";break;
				case 5 :
					$sortby='createtime';break;
			}
			switch($typeval){
				case 1 :$sort='asc';break;
				case 2 :$sort='desc';break;
			}
			$this->listorder=$sortby.' '.$sort;
		}
		parent::_list();
		//dump($this->dao->getLastSQL());exit;
		$this->initView();
		$html=array();
		$list = $this->list;
		
		$schedule=array();
		$ofni=array();
		$db = M('Schedule');
		$formcategory=M('Formcategory')->where(['site_id'=>__SITEID__])->getField('id,name');
		foreach($list as $key => $val){

			//积分公式分解成多维数组，方便前端输出积分修改页
			$list[$key]['formcategory_name']=$formcategory[$val['formcategory_id']];
			$list[$key]['integral'] = $this->dao -> where(array('id' => $val['id'])) -> geteventIntegralArray($this->view);
			
			$schedulemap['mission_id'] = $val['id'];
			$list[$key]['schedule'] = $db -> where($schedulemap) -> select();
			
			
			foreach($list[$key]['schedule'] as $kk => $vv){
				 $list[$key]['schedule'][$kk]['integrals'] = unserialize($vv['integrals']);
			}
			
			
			//$schedulemap['member_id'] = $this->memberInfo['id'];
			//$list[$key]['myschedule'] = $db -> where($schedulemap) -> find();
		}
		//read end
	/*	if($_SESSION['memberInfo']['id']=='118038'){
			dump($list);
			exit();
			}*/
		
		$this->assign("html",$html);
		$this->assign('list', $list);
		$this->assign('schedule',$schedule);
		$this->assign('info',$ofni);

		
		
		$missioncount = array();
		$map = "(reviewer_id='".$this->memberInfo['id']."' or approver_id = ".$this ->memberInfo['id']." or approver like '%,".$this->memberInfo['id'].",%')";
		$missioncount['all'] = $this -> dao->where($map.' and state <> -1') -> count();
		$missioncount['state0'] = $this -> dao->where($map.' and state = 0') -> count();
		$missioncount['state1'] = $this -> dao->where($map.' and state = 1') -> count();
		$missioncount['state2'] = $this -> dao->where($map.' and state = 2') -> count();
		//echo $this->dao -> getLastSql();
		$missioncount['state3'] = $this -> dao->where($map.' and state = 3') -> count();
		$this->assign('missioncount', $missioncount);
		
	    $this->assign('integraltypelist',D('Integral_type')->getType());
		if($this->_param('type')&&$this->_param('type')!='0'){
			$this->display();
			die();
		}
		$this->display('list');
	}
	public function getOne(){
		$this->pagesize = 8;//直接上级或者审批人查看
		$this->listmap = "site_id = ". __SITEID__." and id=".$this->_request('id');
		$InteType = M('Integral_type') -> where(array('site_id' => __SITEID__)) -> select();
		$InteTypeList = array();
		foreach($InteType as $k => $v){
			$InteTypeList[$v['typename']] = $v['remark'];
		}
		
		$this->InteTypeList = $InteTypeList;
		parent::_list();
		$this->initView();
		$html=array();
		$list = $this->list;
		$schedule=array();
		$ofni=array();
		$db = M('Schedule');
		foreach($list as $key => $val){
			//积分公式分解成多维数组，方便前端输出积分修改页
			$list[$key]['integral'] = $this->dao -> where(array('id' => $val['id'])) -> geteventIntegralArray($this->view);
			$schedulemap['mission_id'] = $val['id'];
			$list[$key]['schedule'] = $db -> where($schedulemap) -> select();
			foreach($list[$key]['schedule'] as $kk => $vv){
				 $list[$key]['schedule'][$kk]['integrals'] = unserialize($vv['integrals']);
			}
		}
	    $this->assign('integraltypelist',D('Integral_type')->getType());
		$this->assign("html",$html);
		$this->assign('list', $list);
		$this->assign('schedule',$schedule);
		$this->assign('info',$ofni);
		$this->display('index');
	}
	
	
	//任务的获取工作树的方法
	public function taskMissionTree(){
		if($this->_get('task_id')){
			
			$taskId=$this->_get('task_id');
			
			//先是遍历所有符合条件的工作，然后查找出那些工作的最高层级，并输出
			$pid =isset($_POST['id'])? $_POST['id'] : 0;
			
			$map['pid'] = $pid;
			$map['site_id'] = __SITEID__;
			$map['task_id']=$taskId;
			if($_POST['id']){
				unset($_GET['member_id']);
			}
			if($_GET['member_id']){
				$map['member_id']=$this->_get('member_id');
				unset($map['pid']);
			}
			
			$ret = array();
			$list = $this->dao -> where($map) -> select();
			
			foreach($list as $k => $v){
				//查找层级
				unset($map['member_id']);
				$map['pid']=$v['id'];
				$sub = $this->dao  -> where($map) -> count();
				$isParent = false;
				if($sub > 0){
					$isParent = true;
				}
				$data['isParent'] = $isParent;
				$data['id'] = $v['id'];
				$data['state']=$v['state'];
				$data['name'] = $v['title'];
				$data['object']=unserialize($v['object']);
				$data['form_id']=$v['form_id'];
				$data['createtime']=date('m-d',$v['createtime']);
				$data['endtime']=date('m-d',$v['endtime']);
				$data['proposername']=$v['proposername'];
				$ret[] = $data;
			}
			echo json_encode($ret);
		}
	}
	
	public function missioncount(){
		$missioncount = array();
		//dump($this->dao->filter());
		//$map = "(reviewer_id='".$this->memberInfo['id']."' or approver_id = ".$this ->memberInfo['id']." or approver like '%,".$this->memberInfo['id'].",%')";
		$map=$this->dao->filter();
		$missioncount['all'] = $this -> dao->where($map.' and state <> -1') -> count();
		$missioncount['state0'] = $this -> dao->where($map.' and state = 0') -> count();
		$missioncount['state1'] = $this -> dao->where($map.' and state = 1') -> count();
		$missioncount['state2'] = $this -> dao->where($map.' and state = 2') -> count();
		$missioncount['state3'] = $this -> dao->where($map.' and state = 3') -> count();
		$this->assign('missioncount', $missioncount);
		$this->display();
	}
	
	public function delete(){
		$mission = $this->dao -> where(array('id' => $this->_get('id'))) -> find();
		
		$id = intval($this->_get('id'));
		if($id == 0){
			$this->error('参数错误！');
		}
		$map['id'] = $id;
		$map['site_id'] = __SITEID__;
		$ret = $this->dao -> where($map) -> delete();
		if($ret){
			D('Task') -> where(array('id' => $mission['task_id'], 'site_id' => __SITEID__)) -> SetEventIntegral();
			$this->success('删除成功！');
		}else{
			$this->error($this->dao -> getError());
		}
	}

	public function missionList(){
		if(IS_AJAX){
			$this->initView();
			//D('Mission')->_list($this->view,"(approver like %,".session(C('USER_AUTH_KEY')).",% or )");
			$map = "(reviewer_id='".$this->memberInfo['id']."' or approver_id = ".$this ->memberInfo['id']." or approver like '%,".$this->memberInfo['id'].",%') and site_id = ".__SITEID__;
			$limit=10;
			$order='id desc';
			D('Mission')->_limit($this->view,$map,$order,$limit);
			$this->display();
		}
	}
	
	
	
	public function tree($pid = 0){
		$list = $this->dao -> where(array('site_id' => __SITEID__, 'pid' => $pid)) -> select();
		if($list){
			$this->assign('list', $list);
			$curstr = $this->fetch('TaskMissionList');
			foreach($this->list as $k => $v){
			    return str_replace('<!--sub#'.$v['id'].'-->', $this->tree($v['id']), $curstr);
		    }
		}
	}
	
	
	public function TaskMissionList(){
		if(IS_AJAX){
			$this->initView();
			$taskDB=M('Task');
			
			$task = $taskDB -> where(array('site_id' => __SITEID__, 'id' => $this->_get('task_id'))) -> find();
			$this->info=$this->dao->where(['task_id'=>$_GET['task_id'],'task_hide_id'=>$_GET['task_id']])->getField('id')?false:$task;
			
			if($task['creator_id'] == session(C('USER_AUTH_KEY'))){
				$missionmap = "task_id = '".$this -> _get('task_id')."' and pid = 0";
			}else{
				$missionmap = "task_id = '".$this -> _get('task_id')."' and ((approver_id = '".session(C('USER_AUTH_KEY'))."' or reviewer_id = '".session(C('USER_AUTH_KEY'))."' or approver like '%,".session(C('USER_AUTH_KEY')).",%' and state > 0) or member_id = '".session(C('USER_AUTH_KEY'))."') and pid = 0";
			}
			
			$this->dao->_list($this->view,$missionmap, 'id desc', 20);
			
			//$str = $this->fetch($temp);
			$list = $this->view -> get('list');
			$form = M('Form')->where(['site_id'=>__SITEID__,'name'=>'任务通知单'])->find();
			$field = M('Field') -> where(array('form_id' => $form['id'])) -> order('sort') -> select();
			$this->fields = $field;
			foreach($list as $k => $v){
				$md = unserialize($v['object']);
				
				foreach($field as $key => $val){
					$list[$k]['datalist'][$val['f_name']] = ($val['f_type'] == 8)?date('Y-m-d H:i', $md[$val['f_name']]):($md[$val['f_name']]);
				}
			}
			$this->task=$task;
			$this->assign('list', $list);
			$str = $this->fetch();
			$list = $_GET['list'];
			$empty = 1;
			$statu=$list?1:0;
			if($this->get('list')){
				$empty = 0;
			}
			$this->initView();
			die(json_encode(array('empty' => $empty,'tbody'=>$str,'dump'=>$_REQUEST,'statu'=>$statu, 'pages' => $this-> view -> get('pages'))));	 
		}else{
			$this->display('test');
		}
	}
	
	
	//我填写的审批
	public function mymission(){
	//直接上级或者审批人查看

			$loadIndexUrl = '';
			if(C('URL_MODEL') == 2){
				 $loadIndexUrl = str_replace('index.php/','',__SELF__);
			}else{
				 $loadIndexUrl = __SELF__;
			}
			$per = $_REQUEST;
			unset($per['_URL_']);
			unset($per['theme']);
			unset($per['p']);
			
			//session(MODULE_NAME, $per);
			
			setcookie('loadIndexUrl', U('mymission', $per), time() + 3600, '/');
		$InteType = M('Integral_type') -> where(array('site_id' => __SITEID__)) -> select();
		$InteTypeList = array();
		foreach($InteType as $k => $v){
			$InteTypeList[$v['typename']] = $v['remark'];
		}
		
		$this->InteTypeList = $InteTypeList;
		
		$this->pagesize = 8;
		$this->listmap = "site_id = ". __SITEID__." and (member_id = '".$this -> memberInfo['id']."')";
		$member_id=$this->memberInfo['id'];//会员号
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and ( proposername like '%{$keywords}%' or approvername like '%{$keywords}%' or title like '%{$keywords}%' )";
			$this->keywords=$keywords;
		}
        if($this->_param('status')&&$this->_param('status')!=99||$this->_param('status')=='0'){
			$this->listmap.=" and state = ".$this->_param('status');
			$this->status=$this->_param('status');
		}
        if($this->_param('pagesize')){
            $this->pagesize=$this->_param('pagesize');
            $this->assign('pagesize',$this->pagesize);
        }
        
		if ($this->_param('starttime') || $this->_param('endtime')) {
			$stime = strtotime($this->_param('starttime'));
			$etime = strtotime($this->_param('endtime')) + 24 * 3600;
			if ($this->_param('starttime')) {
				$this->listmap .= " and createtime >= '" . $stime . "'";
				$this->starttime=date('Y-m-d H:i:s',$stime);
			}
			if ($this->_param('endtime')) {
				$this->listmap .= " and createtime <= '" . $etime . "'";
				$this->endtime	=date('Y-m-d H:i:s',$etime);
			}
			if ($this->_param('starttime') && $this->_param('endtime')) {
				$this->listmap .= " and createtime <= '" . $etime . "'";
				$this->listmap .= " and createtime >= '" . $stime . "'";
				$this->starttime=date('Y-m-d H:i:s',$stime);
				$this->endtime	=date('Y-m-d H:i:s',$etime);
			}
		}
		
		if(isset($_GET['state'])){
			if($_GET['state'] != 'all'){
				$this->listmap .= " and state = '" . intval($_GET['state']) . "'";
			}
		}
		if($this->_param('id')){
			$id=$this->_param('id');
			$this->listmap.= ' and formcategory_id = '.$id;
		}
		$category=D('formcategory')->select();//查找分类
		foreach($category as $key => $value){
			$map=$this->listmap;
			$map .= ' and formcategory_id = '.$value['id'];
			$category[$key]['count']=D('mission')->where($map)->count();
		}
		$this->assign('candy',$category);//分类渲染
		
		parent::_list();
		$this->initView();
		$html=array();
		$list = $this->list;
		
		$schedule=array();
		$ofni=array();
		$db = M('Schedule');
		$formcategory=M('Formcategory')->where(['site_id'=>__SITEID__])->getField('id,name');
		foreach($list as $key => $val){

			
			//积分公式分解成多维数组，方便前端输出积分修改页
			
			$list[$key]['formcategory_name']=$formcategory[$val['formcategory_id']];
			$list[$key]['integral'] = $this->dao -> where(array('id' => $val['id'])) -> geteventIntegralArray($this->view);
			
			$schedulemap['mission_id'] = $val['id'];
			$list[$key]['schedule'] = $db -> where($schedulemap) -> select();
			
			
			foreach($list[$key]['schedule'] as $kk => $vv){
				 $list[$key]['schedule'][$kk]['integrals'] = unserialize($vv['integrals']);
			}
			
			
			//$schedulemap['member_id'] = $this->memberInfo['id'];
			//$list[$key]['myschedule'] = $db -> where($schedulemap) -> find();
		}
		$this->assign("html",$html);
		$this->assign('list', $list);
		$this->assign('schedule',$schedule);
		$this->assign('info',$ofni);
		
		if($this->_param('type')&&$this->_param('type')!='0'){
			$this->display();
			die();
		}
		$this->display('list');
	}
	//查看审批
	public function mymissionread(){
		$this->initView();
		$id = $this->_get('id', 'intval');
		//查找一条记录
		$map['id'] = $id;
		$map['site_id'] = __SITEID__;
		$info = $this -> dao -> where($map) -> find();
		//表单数据
		$formdata = unserialize($info['object']);
		//表单模板
		$formtemplate = $info['formtemplate'];
		//还原表单
		$this->html = $formtemplate;
		$db = M('Schedule');
		$schedulemap['mission_id'] = $id;
		$schedulemap['member_id'] = $this->memberInfo['id'];
		$this->schedule = $db -> where($schedulemap) -> find();
		$this->assign('info', $info);
		$this->display();
	}
	//查看审批,可以直接审批通过,界面跟审批中心一样
	public function read(){
		$this->initView();
		
		//任务审批，读取负责人审批
		if($this->_get('task_id','intval')){
			$taskDB=M('Task');
			$map['task_hide_id']=$this->_get('task_id');
			$this->dao->addMission($taskDB->where(['id'=>$map['task_hide_id']])->find(),$this->memberInfo);
		}else{
			$id = $this->_get('id', 'intval');
			//查找一条记录
			$map['id'] = $id;
		}
		
		$map['site_id'] = __SITEID__;
		$info = $this -> dao -> where($map) -> find();
		
		$InteType = M('Integral_type') -> where(array('site_id' => __SITEID__)) -> select();
		$InteTypeList = array();
		foreach($InteType as $k => $v){
			$InteTypeList[$v['typename']] = $v['remark'];
		}
		
		$this->InteTypeList = $InteTypeList;
		
		$info['integral'] = $this->dao -> where(array('id' => $info['id'])) -> geteventIntegralArray($this->view);
			
		$schedulemap['mission_id'] = $info['id'];
		$info['schedule'] = M('Schedule') -> where($schedulemap) -> select();
		
		
		foreach($info['schedule'] as $kk => $vv){
			 $info['schedule'][$kk]['integrals'] = unserialize($vv['integrals']);
		}
		
		
		
		
		
		//表单数据
		$formdata = unserialize($info['object']);
		//表单模板
		$formtemplate = replacebr($info['formtemplateText']);
		//还原表单
		$this->html = $formtemplate;
		$db = M('Schedule');
		$schedulemap['mission_id'] = $id;
		$this->schedule = $db -> where($schedulemap) -> find();
		$this->assign('info', $info);
		$this->display();
	}
	
	//可以修改审批
	public function edit(){
		$this->initView();
		$id = $this->_get('id', 'intval');
		//查找一条记录
		$map['id'] = $id;
		$map['site_id'] = __SITEID__;
		$info = $this -> dao -> where($map) -> find();
		//表单数据
		$formdata = unserialize($info['object']);
		//表单模板
		$formtemplate = $info['formtemplate'];
		//还原表单
		$this->html = $formtemplate;
		$db = M('Schedule');
		$schedulemap['mission_id'] = $id;
		$this->schedule = $db -> where($schedulemap) -> find();
		$this->assign('info', $info);
		$this->display();
	}
	//查看审批
	public function mymissiontext(){
		$this->initView();
		$id = $this->_get('id', 'intval');
		//查找一条记录
		$map['id'] = $id;
		$map['site_id'] = __SITEID__;
		$info = $this -> dao -> where($map) -> find();
		//表单数据
		$formdata = unserialize($info['object']);
		//表单模板
		$formtemplate = $info['formtemplate'];
		//还原表单
		$this->html = $formtemplate;
		$db = M('Schedule');
		$schedulemap['mission_id'] = $id;
		$schedulemap['member_id'] = $this->memberInfo['id'];
		$this->schedule = $db -> where($schedulemap) -> find();
		$this->assign('info', $info);
		$this->display();
	}
		
	
	//获取当前用户最近使用的表格
	public function getRecentForm(){
		$mission=$this->dao->where(['user_id'=>$this->UserInfo,'site_id'=>__SITEID__])->group('form_id')->limit(10)->select();
		$form=[];
		$formDB=D('Form');
		foreach($mission as $vo){
			$form[]=$formDB->where('id='.$vo['form_id'])->find();
		}
		$this->list=$form;
		$this->display();
	}
	
	//获取当前用户最近操作过的人员
	public function getRecentFormMember(){
		$mission=$this->dao->where(['user_id'=>$this->UserInfo])->group('member_id')->limit(10)->select();
		$member=[];
		$memberDB=D('Member');
		foreach($mission as $vo){
			$r=$memberDB->where('id='.$vo['member_id'])->find();
			if($r){ $member[]=$r; }
		}
		$this->list=$member;
		$this->display();
	}
	
	//申请积分审批的归档动作
	public function choiceForm(){
		$this->assign('formcategorylist',D('Formcategory')->getList());
		$this->display();	
	}
	
	//归档后的提交
	public function changeForm(){
		if(IS_POST){
			$this->dao->create($_POST);	
			$ret=$this->dao->save();
			if($ret){
				$_POST['status']=1;
				$_POST['mission_id']=$_POST['id'];
				unset($_POST['id']);
				echo(json_encode(['status'=>1,'state'=>1]));
				//$this->pass();
			}else{
				$this->error('归档错误，请重试！');
			}
		}
	}
	
	public function change(){
		if($_GET['id']){
			$this->list=$this->dao->where('id='.$_GET['id'])->select();
			$this->display();	
		}	
	}
	
	//递交表格
	public function send(){
		if($this->_post('id')){
			$mission = $this -> dao -> where(array('id' => $this->_post('id'))) -> find();
			if($mission['state'] >= 0){
				$this->error('该审批已经递交，请不要重复操作');
			}else{
				$ret = $this -> dao -> where(array('id' => $this->_post('id'))) -> save(array('state' => 1));
				if($ret){
					$this->success('递交成功');
				}else{
					$this->error($this->dao -> getError());
				}
			}
		}else{
			$this->error('参数错误!');
		}
		
	}
	
	//取消申请
	public function cancel(){
		if($this->_post('id')){
			$mission = $this -> dao -> where(array('id' => $this->_post('id'))) -> find();
			if($mission['state'] == 3){
				$this->error('已经通过的审批不能取消！');
			}else{
				$ret = $this -> dao -> where(array('id' => $this->_post('id'))) -> save(array('state' => -1));
				if($ret){
					$this->success('取消成功');
				}else{
					$this->error($this->dao -> getError());
				}
			}
		}else{
			$this->error('参数错误!');
		}
		
	}
	
	
	//查看审批记录
	public function schedule(){
	
		if($this->_get('id')){
			$InteType = M('Integral_type') -> where(array('site_id' => __SITEID__)) -> select();
			$InteTypeList = array();
			foreach($InteType as $k => $v){
				$InteTypeList[$v['typename']] = $v['remark'];
			}
			$this->InteTypeList = $InteTypeList;
			$db = M('Schedule');
			$memberdb = M('Member');
			$list = $db -> where(array('mission_id' => $this->_get('id'))) -> select();
			foreach($list as $k => $v){
				$list[$k]['nickname'] = $memberdb -> where(array('id' => $v['member_id'])) -> getField('name');
				$list[$k]['integrals'] = unserialize($v['integrals']);
				//dump($list[$k]['integrals']);
				
			}
			
			$this->assign('list', $list);
			if($this->_param('check')&&$this->_param('check')=='1'){
				$this->display('myOne');
				die();
			}
			$this->display();
			
		}else{
			$this->error('参数错误!');
		}
		
	}
	
	//查看审批记录
	public function sendTo(){
		if(IS_POST){
			$type = $_POST['type'];
			if(count($_POST['member_id']) > 1 && $type == 0){
				$type = 1;
			}
			foreach($_POST['member_id'] as $v){
				$this-> dao -> where(array('id' => $_POST['id'])) -> sendTo($v, $type);
			}
			if(count($_POST['member_id']) > 1 && $type == 0){
				$this-> dao -> where(array('id' => $_POST['id'])) -> delete();
			}
			$this->success('发送成功');
		}else{
			$this -> info = $this->dao -> where(array('id' => $this->_get('id'))) -> find();
			$this->display();
		}
	}
	
	//通过审批
	public function pass(){
		 
		$this->initView();
		$ret = $this->dao -> pass($_POST, $this->view);
		if($ret === false){
			$this->error($this->dao -> getError());
		}
		die(json_encode($ret));
		//$this->success('操作成功');
		return;
	}
	
	
	public function apply(){
		if(IS_POST){
			$memberDB=D('Member');
			$formula = array();
			$integraltype = M('Integral_type') -> where(array('site_id' => __SITEID__)) -> select();
			foreach($integraltype as $k => $v){
			    $formula[] = "proposer:".$v['typename']."=".intval($_POST[$v['typename']]);
				$value[] = '申请'.$v['remark'].'：'.intval($_POST[$v['typename']]);
				$integralret[$v['typename']]=0;
			}
			$data['formula'] = implode(',', $formula);
			$data['formtemplate'] = $_POST['comment'];
			$data['formtemplateText'] = $_POST['comment'];
			$data['form_id'] = 0;
			$data['state']=1;
			$data['member_id'] = $this -> memberInfo['id'];
			$data['approver_id'] = $this->memberInfo['pid'];
			//$data['reviewer_id'] = $this->memberInfo['pid'];
			$approver = getManagerList();
			$approver[]=$this->memberInfo['pid'];
			//由于getmanagerList会输出所有的ID，在输出之前查询member表是否有此ID，并输出
			//$data['approver'] = ','.implode(',', $approver).',';
			if(trim(implode('',$approver))==''){
				$this->error('暂无审核人，请重试。');
			}
			$data['approver']= ','.implode(',',$memberDB->where('id in ('.implode(',', $approver).') and site_id='.__SITEID__)->getField('id',true)).',';
			
			$pName=$memberDB->where('id='.$data['approver_id'])->getField('name');
			
			$data['approvername']=isset($_POST['approvername']) ? implode('，',$_POST['approvername']) : $pName;
			$data['proposername']=$this->memberInfo['name'] != '' ? $this->memberInfo['name'] : $this->memberInfo['nickname'];
			$data['missionType'] = 1;
			$data['title'] = '积分申请表';
			$data['record'] = build_order_no();
			$data['site_id'] = __SITEID__;
			$data['createtime'] = time();
			$data['detail'] = $_POST['comment'];
			
			$data['integralret']=serialize([$data['member_id']=>['name'=>$data['proposername'],'integral'=>$integralret]]);
			
			$missiondb = M('Mission');
			$ret = $missiondb->add($data);
			if($ret){
				$this->success('提交成功，等待审核！');
			}else{
				$this->error($missiondb -> getError());
			}
		}else{
			//如果系统没有设置积分管理员，要怎么处理呢
			//已处理
			$memberDB=D('Member');
			$approver = getManagerList();
			$approver[] = $this->memberInfo['pid'];
			if(!empty($approver)){
				$this -> approvername = $memberDB->where('id in ('.implode(',',$approver).') and site_id='.__SITEID__)->Field('name') -> select();
				//dump($memberDB->getLastSQL());exit;
			}
			$this->integraltype = M('Integral_type') -> where(array('site_id' => __SITEID__)) -> select();
			$this -> display();
		}
	}
	
}